package cn.shopping.core.controller;

import cn.shopping.common.bo.PageBO;
import cn.shopping.common.controller.BaseController;
import cn.shopping.common.dto.IdDTO;
import cn.shopping.common.util.CommonUtils;
import cn.shopping.common.vo.ResponseVO;
import cn.shopping.core.dto.DeleteGoodsIdDTO;
import cn.shopping.core.dto.GoodsDTO;
import cn.shopping.core.dto.PageListGoodsDTO;
import cn.shopping.core.dto.UpdateGoodsDTO;
import cn.shopping.core.service.GoodsService;
import cn.shopping.core.vo.GoodsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述:
 *
 * @author Zhanggq
 * @date 2019/4/4 16:09
 */
@RestController
@RequestMapping(value = "/webapi/goods/", method = RequestMethod.POST)
@Api(description = "商品控制层")
public class GoodsController extends BaseController {
    private static final Logger logger= LoggerFactory.getLogger(GoodsController.class);

    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "getGoodsById")
    @ApiOperation(value = "根据ID获取商品对象，包括图片",notes = "根据ID获取商品对象,包括图片")
    public ResponseVO<GoodsDTO> getGoodsById(@RequestBody IdDTO<Long> idDTO) {
        return CommonUtils.okResponseVO(this.goodsService.getGoodsByGoodsId(idDTO.getId()));
    }

    @RequestMapping(value = "insertGoods")
    @ApiOperation(value = "新增商品", notes = "该接口返回一个Long类型的商品id")
    public ResponseVO<Long> insertGoods(@RequestBody GoodsDTO dto) {
        return CommonUtils.okResponseVO(this.goodsService.insertGoods(dto));
    }

    @RequestMapping(value = "updateGoodsById")
    @ApiOperation(value = "修改商品", notes = "该接口返回一个Long类型的商品id")
    public ResponseVO<Long> updateGoodsById(@RequestBody UpdateGoodsDTO dto) {
        return CommonUtils.okResponseVO(this.goodsService.updateGoodsById(dto));
    }

    @RequestMapping(value = "deleteGoodsByIds")
    @ApiOperation(value = "批量删除商品", notes = "传一个/多个商品ID，图片也会被删除")
    public ResponseVO<Integer> deleteGoodsByIds(@RequestBody DeleteGoodsIdDTO dto) {
        return CommonUtils.okResponseVO(this.goodsService.deleteGoodsByIds(dto));
    }


    @RequestMapping("pageListGoods")
    @ApiOperation(value = "按条件分页查询",notes = "按条件分页查询")
    public ResponseVO<PageBO<GoodsVO>> pageListGoods(@RequestBody PageListGoodsDTO dto){
        return CommonUtils.okResponseVO(this.goodsService.pageListGoods(dto));
    }

}
